<template>
    <div class="app-container">
        <!--工具栏-->
        <div class="head-container">
            <div v-if="crud.props.searchToggle">
                <!-- 搜索 -->
                <el-input v-model="query.blurry" clearable size="small" placeholder="模糊搜索" style="width: 200px;"
                    class="filter-item" @keyup.enter.native="toQuery" />
                <el-date-picker v-model="query.createTime" :default-time="['00:00:00', '23:59:59']" type="daterange"
                    range-separator=":" size="small" class="date-item" value-format="yyyy-MM-dd HH:mm:ss"
                    start-placeholder="开始日期" end-placeholder="结束日期" />
                <rrOperation />
            </div>
            <crudOperation :permission="permission" />
        </div>
        <!--表单渲染-->
        <el-dialog append-to-body :close-on-click-modal="false" :before-close="crud.cancelCU"
            :visible.sync="crud.status.cu > 0" :title="crud.status.title" width="700px">
            <el-form ref="form" :inline="true" :model="form" :rules="rules" size="small" label-width="80px">
                <el-form-item label="菜单类型" prop="menuType">
                    <el-radio-group v-model="form.menuType" size="mini" style="width: 178px">
                        <el-radio-button label="1">目录</el-radio-button>
                        <el-radio-button label="2">菜单</el-radio-button>
                        <el-radio-button label="3">按钮</el-radio-button>
                    </el-radio-group>
                </el-form-item>
                <el-form-item v-show="form.menuType.toString() !== '3'" label="菜单图标" prop="menuIcon">
                    <el-popover placement="bottom-start" width="450" trigger="click"
                        @show="$refs['iconSelect'].reset()">
                        <IconSelect ref="iconSelect" @selected="selected" />
                        <el-input slot="reference" v-model="form.menuIcon" style="width: 450px;" placeholder="点击选择图标"
                            readonly>
                            <svg-icon v-if="form.menuIcon" slot="prefix" :icon-class="form.menuIcon"
                                class="el-input__icon" style="height: 32px;width: 16px;" />
                            <i v-else slot="prefix" class="el-icon-search el-input__icon" />
                        </el-input>
                    </el-popover>
                </el-form-item>
                <el-form-item v-show="form.menuType.toString() !== '3'" label="外链菜单" prop="iframe">
                    <el-radio-group v-model="form.iframe" size="mini">
                        <el-radio-button label="true">是</el-radio-button>
                        <el-radio-button label="false">否</el-radio-button>
                    </el-radio-group>
                </el-form-item>
                <el-form-item v-show="form.menuType.toString() === '2'" label="菜单缓存" prop="cache">
                    <el-radio-group v-model="form.cache" size="mini">
                        <el-radio-button label="true">是</el-radio-button>
                        <el-radio-button label="false">否</el-radio-button>
                    </el-radio-group>
                </el-form-item>
                <el-form-item v-show="form.menuType.toString() !== '3'" label="菜单可见" prop="hidden">
                    <el-radio-group v-model="form.hidden" size="mini">
                        <el-radio-button label="false">是</el-radio-button>
                        <el-radio-button label="true">否</el-radio-button>
                    </el-radio-group>
                </el-form-item>
                <el-form-item v-if="form.menuType.toString() !== '3'" label="菜单标题" prop="title">
                    <el-input v-model="form.title" :style="form.type === '0' ? 'width: 450px' : 'width: 178px'"
                        placeholder="菜单标题" />
                </el-form-item>
                <el-form-item v-if="form.menuType.toString() === '3'" label="按钮名称" prop="title">
                    <el-input v-model="form.title" placeholder="按钮名称" style="width: 178px;" />
                </el-form-item>
                <el-form-item label="权限标识" prop="permission">
                    <el-input v-model="form.permission" :disabled="form.iframe" placeholder="权限标识"
                        style="width: 300px;" />
                </el-form-item>
                <el-form-item v-if="form.menuType.toString() !== '3'" label="路由地址" prop="path">
                    <el-input v-model="form.path" placeholder="路由地址" style="width: 350px;" />
                </el-form-item>
                <el-form-item label="菜单排序" prop="sortNo">
                    <el-input-number v-model.number="form.sortNo" :min="0" :max="999" controls-position="right"
                        style="width: 178px;" />
                </el-form-item>
                <el-form-item v-show="!form.iframe && form.menuType.toString() === '2'" label="组件名称" prop="name">
                    <el-input v-model="form.name" style="width: 178px;" placeholder="匹配组件内Name字段" />
                </el-form-item>
                <el-form-item v-show="!form.iframe && form.menuType.toString() === '2'" label="组件路径" prop="component">
                    <el-input v-model="form.component" style="width: 400px;" placeholder="组件路径" />
                </el-form-item>
                <el-form-item label="上级类目" prop="parentId">
                    <treeselect v-model="form.parentId" :options="menus" :load-options="loadMenus" style="width: 450px;"
                        placeholder="选择上级类目" />
                </el-form-item>
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button type="text" @click="crud.cancelCU">取消</el-button>
                <el-button :loading="crud.status.cu === 2" type="primary" @click="crud.submitCU">确认</el-button>
            </div>
        </el-dialog>
        <!--表格渲染-->
        <el-table ref="table" v-loading="crud.loading" lazy :load="getMenus" :data="crud.data[crud.listName]"
            :tree-props="{ children: 'children', hasChildren: 'hasChildren' }" row-key="id" @select="crud.selectChange"
            @select-all="crud.selectAllChange" @selection-change="crud.selectionChangeHandler">
            <el-table-column type="selection" width="55" />
            <el-table-column :show-overflow-tooltip="true" label="菜单标题" width="240px" prop="title" />
            <el-table-column prop="menuIcon" label="图标" align="center" width="60px">
                <template slot-scope="scope">
                    <svg-icon :icon-class="scope.row.menuIcon ? scope.row.menuIcon : ''" />
                </template>
            </el-table-column>
            <el-table-column prop="sortNo" align="center" label="排序">
                <template slot-scope="scope">
                    {{ scope.row.sortNo }}
                </template>
            </el-table-column>
            <el-table-column :show-overflow-tooltip="true" prop="permission" label="权限标识" />
            <el-table-column :show-overflow-tooltip="true" prop="component" label="组件路径" />
            <el-table-column prop="iframe" label="外链" width="75px">
                <template slot-scope="scope">
                    <span v-if="scope.row.iframe">是</span>
                    <span v-else>否</span>
                </template>
            </el-table-column>
            <el-table-column prop="cache" label="缓存" width="75px">
                <template slot-scope="scope">
                    <span v-if="scope.row.cache">是</span>
                    <span v-else>否</span>
                </template>
            </el-table-column>
            <el-table-column prop="hidden" label="可见" width="75px">
                <template slot-scope="scope">
                    <span v-if="scope.row.hidden">否</span>
                    <span v-else>是</span>
                </template>
            </el-table-column>
            <el-table-column prop="createTime" label="创建日期" width="135px">
                <template slot-scope="scope">
                    <span>{{ parseTime(scope.row.createTime) }}</span>
                </template>
            </el-table-column>
            <el-table-column v-permission="['admin', 'menu/edit', 'menu/delete']" label="操作" width="130px"
                align="center" fixed="right">
                <template slot-scope="scope">
                    <udOperation :data="scope.row" :permission="permission" msg="确定删除吗,如果存在下级节点则一并删除，此操作不能撤销！" />
                </template>
            </el-table-column>
        </el-table>
    </div>
</template>

<script>
import crudMenu from '@/api/system/menu'
import IconSelect from '@/components/IconSelect'
import Treeselect from '@riophae/vue-treeselect'
import '@riophae/vue-treeselect/dist/vue-treeselect.css'
import { LOAD_CHILDREN_OPTIONS } from '@riophae/vue-treeselect'
import CRUD, { presenter, header, form, crud } from '@crud/crud'
import rrOperation from '@crud/RR.operation'
import crudOperation from '@crud/CRUD.operation'
import udOperation from '@crud/UD.operation'

// crud交由presenter持有
const defaultForm = { id: null, title: null, sortNo: 999, path: null, component: null, name: null, iframe: false, roles: [], parentId: 0, menuIcon: null, cache: false, hidden: false, menuType: 1, permission: null }
export default {
    name: 'Menu',
    components: { Treeselect, IconSelect, crudOperation, rrOperation, udOperation },
    cruds() {
        return CRUD({
            title: '菜单',
            url: 'admin/menu/list',
            crudMethod: { ...crudMenu },
            optShow: { reset: true, add: true, edit: false, del: false, download: false },
            query: {
                pageIndex: 1,
                pageSize: 100,
            }
        })
    },
    mixins: [presenter(), header(), form(defaultForm), crud()],
    data() {
        return {
            menus: [],
            permission: {
                add: ['admin', 'menu/add'],
                edit: ['admin', 'menu/edit'],
                del: ['admin', 'menu/delete'],
                export: ['admin', 'menu/export']
            },
            rules: {
                title: [
                    { required: true, message: '请输入标题', trigger: 'blur' }
                ],
                path: [
                    { required: true, message: '请输入地址', trigger: 'blur' }
                ],
                permission: [
                    { required: true, message: '请输入标示', trigger: 'blur' }
                ]
            }
        }
    },
    created() {
    },
    mounted() {
        console.log("123132131", setTimeout(() => {
            this.crud.data[crud.listName]
        }, 100));

    },
    methods: {
        // 新增与编辑前做的操作
        [CRUD.HOOK.afterToCU](crud, form) {
            this.menus = []
            if (form.id != null) {
                // form.parentId = 0
                // this.getSupDepts(form.id)
                this.resetNull(crud.data.content)
            } else {
                this.menus.push({ id: 0, label: '顶级类目', children: null })
            }
        },
        getMenus(tree, treeNode, resolve) {
            const params = { parentId: tree.id || '', pageIndex: 1, pageSize: 100 }
            setTimeout(() => {
                crudMenu.getMenus(params).then(res => {
                    resolve(res.content)
                })
            }, 100)
        },
        getSupDepts(id) {
            crudMenu.getMenus({ parentId: 0, pageIndex: 1, pageSize: 100 }).then(res => {
                const children = res.data.list.map(function (obj) {
                    obj.children = null
                    return obj
                })

                this.menus = [{ id: 0, label: '顶级类目', children: children }]
            })
        },
        loadMenus({ action, parentNode, callback }) {
            if (action === LOAD_CHILDREN_OPTIONS) {
                crudMenu.getMenus({ parentId: parentNode.id, pageIndex: 1, pageSize: 100 }).then(res => {
                    parentNode.children = res.content.map(function (obj) {
                        obj.children = null
                        return obj
                    })
                    setTimeout(() => {
                        callback()
                    }, 100)
                })
            }
        },
        // 选中图标
        selected(name) {
            this.form.menuIcon = name
        },

        resetNull(data) {
            const cpdata = JSON.parse(JSON.stringify(data))
            const getchild = (datas) => {
                datas.map(item => {
                    if (!item.children || item.children.length === 0) {
                        item.children = null
                    } else {
                        getchild(item.children)
                    }
                })
            }
            getchild(cpdata)
            this.menus = [{ id: 0, label: '顶级类目', children: cpdata }]
            // return getchild(data)
        },
    }
}
</script>

<style rel="stylesheet/scss" lang="scss" scoped>
/deep/ .el-input-number .el-input__inner {
    text-align: left;
}

/deep/ .vue-treeselect__control,
/deep/ .vue-treeselect__placeholder,
/deep/ .vue-treeselect__single-value {
    height: 30px;
    line-height: 30px;
}
</style>
